package org.apache.struts.pharmacy.pharmacy.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.pharmacy.pharmacy.action.SearchResultsAction;
import org.apache.struts.pharmacy.pharmacy.model.*;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.TBR_R08;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.earogya.communicate.Communicator;
import com.earogya.logic.pharmacy.request.CreateEQQQ04;
import com.earogya.logic.pharmacy.request.TBR_R08_Handler;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/***
 * Class consists with the struts action to drugs given to a patient.
 * @author klanerolle@gmail.com
 *
 */
public class DrugsGivenToPatientAction extends ActionSupport {

	private PrescriptionDrugs prescriptionDrugs;
	private HL7Message message;
	private Date date;
	private SearchResultsAction action;

	public HL7Message getMessage() {
		return message;
	}

	public PrescriptionDrugs getPrescriptionDrugs() {
		return prescriptionDrugs;
	}

	public void setPrescriptionDrugs(PrescriptionDrugs prescriptionDrugs) {
		this.prescriptionDrugs = prescriptionDrugs;
	}

	public SearchResultsAction getAction() {
		return action;
	}

	public void setAction(SearchResultsAction action) {
		this.action = action;
	}

	/**
	 * Overrides the execute method is ActionSupport interface,When the action occurs this method is executed.
	 * returns ERROR if there is an error while sending, receiving or processing the received message.
	 * returns SUCCESS if there are no errors.
	 */
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		CreateEQQQ04 createMessage = new CreateEQQQ04(prescriptionDrugs);
		String msg = createMessage.createMessage((String) session
				.get("facility"));
		message = new HL7Message();
		if (!msg.equals(null)) {
			Communicator c = new Communicator();
			String reply = c.sendMessage(msg);

			if (!reply.contains("RDT|no results found")){
				message.setMessage(msg + "-------------" + reply);
				System.out.print(reply);
				action = new SearchResultsAction(reply);
				action.execute();
			} else {
				message.setMessage("No Results Found");
				return ERROR;
			}
		} else {
			message.setMessage("failed to create message");
			return ERROR;
		}
		message.setMessage("empty");
		return SUCCESS;
	}

	/**
	 * Overrides the validate method in ActionSupport interface.
	 * Validates the input fields for correct input.
	 */
	public void validate() {
		if (prescriptionDrugs.getPatientId().length() == 0) {
			addFieldError("prescriptionDrugs.patientId", "Patient ID required");
		}else if(!this.isInteger(prescriptionDrugs.getPatientId())){
			addFieldError( "prescriptionDrugs.patientId", "Incorrect patient ID format." );
		}
	}

	/**
	 * Process the TBR_R08 query message and received the prescription drugs list values.
	 * @param hapiMsg to parse the object of an TBR_R08 query message
	 * @throws EncodingNotSupportedException, HL7Exception
	 */
	public TBR_R08 createTBR_R08Msg(String message)
			throws EncodingNotSupportedException, HL7Exception {
		Message hapiMsg = new PipeParser().parse(message);
		return (TBR_R08) hapiMsg;
	}
	
	/**
	 * Check if the given string is an integer.
	 * @param value
	 * @return true is the value is an integer
	 */
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
}
